package ThreadExample1;

public class RunnableImpl implements Runnable {

    //2.创建一个同步代码锁对象
    Object obj = new Object();
    //1.定义多线程共享的数据
    private int ticket = 2000;

    @Override
    public void run() {
        //设置多线程
        while (true) {
            synchronized (obj) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "正在卖" + ticket + "票");
                    ticket--;
                }
            }
        }

    }
}
